package com.xy.smartsms.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by HuangYueHui on 2017/5/22.
 */

public class TimeUtils {


    /**
     * 获取过期时间 deadline=【timeline】+6h
     *
     * @param timeline 排序时间
     * @return
     */
    public static long getHeadLine(long timeline, int hour) {
        try {
            Date date = longToDate(timeline, "yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR, hour);
            date = calendar.getTime();
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToString(long time, String formatType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    /**
     * long转换为Date类型
     *
     * @param time       要转换的long类型的时间
     * @param formatType 要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     * @return
     * @throws ParseException
     */
    public static Date longToDate(long time, String formatType)
            throws ParseException {
        Date dateOld = new Date(time); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }


    /**
     * date类型转换为String类型
     *
     * @param date       类型的时间
     * @param formatType 格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     * @return
     */
    public static String dateToString(Date date, String formatType) {
        return new SimpleDateFormat(formatType).format(date);
    }

    /**
     * String类型转换为Long类型
     *
     * @param time       "yyyy-MM-dd"
     * @param formatType 格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     * @return
     * @throws ParseException
     */
    public static long stringToLong(String time, String formatType) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        return simpleDateFormat.parse(time).getTime();
    }

    /**
     * String类型转换为date类型
     *
     * @param time       要转换的string类型的时间
     * @param formatType 要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String time, String formatType) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(time);
        return date;
    }

    public static long buildStamp(long reference, String date, String time) {
        if (reference == 0L) {
            reference = System.currentTimeMillis();
        }

        String year = stampToString(reference, "yyyy");
        long stamp = reference;
        try {
            stamp = stringToLong(year + date + time, "yyyyMM月dd日HH:mm");
            if (stamp < reference) {
                year = String.valueOf(Integer.parseInt(year) + 1);
                stamp = stringToLong(year + date + time, "yyyyMM月dd日HH:mm");
            }
        } catch (Exception e) {

        }

        return stamp;
    }
}
